/***************************************************************************
 *   Copyright (C) 2009 by Zheng Yonglei
 *   zhengyonglei@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "ReciteTest_YesNo.h"

ReciteTest_YesNo::ReciteTest_YesNo(WordBrowser* display, QWidget* parent):
	ReciteTestMethodBase(display, parent),
	curButtonGroup(1)
	//enableSpeak(true),
	//enableArticleNav(true)
{
	firstPrompt = new QLabel(tr("Do you recognize this word?"));
	firstPrompt->setWordWrap(true);

	Yes1 = new QPushButton(tr("Yes [A]"), this);
	Yes1->setIcon(QIcon("./pixmaps/agt_action_success-256.png"));
	Yes1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	Yes1->setMinimumSize(120,60);
	Yes1->setMaximumSize(140,80);
	Yes1->setFont(mFont);
	//Yes1->setPalette(QPalette(QColor(Qt::green)));
	//Yes1->setAutoFillBackground(true);
	Yes1->hide();

	No1 = new QPushButton(tr("No [D]"), this);
	No1->setIcon(QIcon("./pixmaps/cancel-256.png"));
	No1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	No1->setMinimumSize(120,60);
	No1->setMaximumSize(140,80);
	No1->setFont(mFont);
	//No1->setPalette(QPalette(QColor(Qt::red)));
	//No1->setAutoFillBackground(true);
	No1->hide();

	testMethodGrid->addWidget(firstPrompt, 1, 1);
	testMethodGrid->addWidget(Yes1, 2, 1);
	testMethodGrid->addWidget(No1, 3, 1);

	secondPrompt = new QLabel(tr("Are you right?"));
	secondPrompt->setWordWrap(true);

	Yes2 = new QPushButton(tr("Yes [A]"), this);
	Yes2->setIcon(QIcon("./pixmaps/agt_action_success-256.png"));
	Yes2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	Yes2->setMinimumSize(120,60);
	Yes2->setMaximumSize(140,80);
	Yes2->setFont(mFont);
	//Yes2->setPalette(QPalette(QColor(Qt::green)));
	//Yes2->setAutoFillBackground(true);
	Yes2->hide();

	No2 = new QPushButton(tr("No [D]"), this);
	No2->setIcon(QIcon("./pixmaps/cancel-256.png"));
	No2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	No2->setMinimumSize(120,60);
	No2->setMaximumSize(140,80);
	No2->setFont(mFont);
	//No2->setPalette(QPalette(QColor(Qt::red)));
	//No2->setAutoFillBackground(true);
	No2->hide();

	testMethodGrid->addWidget(secondPrompt, 1, 1);
	testMethodGrid->addWidget(Yes2, 2, 1);
	testMethodGrid->addWidget(No2, 3, 1);

	QObject::connect(Yes1,SIGNAL(clicked()),this,SLOT(showRememberButton()));
	QObject::connect(Yes2,SIGNAL(clicked()),this,SLOT(remember()));

	QObject::connect(No1,SIGNAL(clicked()),this,SLOT(forget()));
	QObject::connect(No2,SIGNAL(clicked()),this,SLOT(forget()));

	Yes1ShortCut = new QShortcut(QKeySequence(Qt::Key_A),  this);
	No1ShortCut = new QShortcut(QKeySequence(Qt::Key_D),  this);
	QObject::connect(Yes1ShortCut,SIGNAL(activated()),Yes1,SIGNAL(clicked()));
	QObject::connect(No1ShortCut,SIGNAL(activated()),No1,SIGNAL(clicked()));

	Yes2ShortCut = new QShortcut(QKeySequence(Qt::Key_A),  this);
	No2ShortCut = new QShortcut(QKeySequence(Qt::Key_D),  this);
	QObject::connect(Yes2ShortCut,SIGNAL(activated()),Yes2,SIGNAL(clicked()));
	QObject::connect(No2ShortCut,SIGNAL(activated()),No2,SIGNAL(clicked()));

	mTimer = new QTimer(this);
	mTimeBar = new QProgressBar(this);
	mTimeBar->setTextVisible(false);
	mTimeBar->setMaximumHeight(8);
	QObject::connect(mTimer, SIGNAL(timeout()), this, SLOT(updateTimeBar()));
	testMethodGrid->addWidget(mTimeBar, 4, 1);
}

ReciteTest_YesNo::~ReciteTest_YesNo()
{
}

void ReciteTest_YesNo::beginTest()
{
	//cout<<"ReciteTest_YesNo::beginTest"<<endl;
	//enableSpeak = OPT.enableSpeak;
	OPT.lockSpeak = true;
	OPT.lockArticleNav = true;
	OPT.lockHistoryNav = true;
	OPT.lockWordBrowserSearch = true;
	OPT.lockSwitchTask = true;
	ReciteTestMethodBase::beginTest();
	textDisplay->hideArticleNav();
	QString format = QString("<h1 align=\"center\">%1</h1>");
	//textDisplay->getTextBrowser()->clear();
	textDisplay->clearDisplay();
	textDisplay->showEnglish(-1, format);
	//textDisplay->display();
	DM.lookup(WM.getCurWord());
	showFirstYesNoButtonGroup();
}

void ReciteTest_YesNo::endTest()
{
	//cout<<"ReciteTest_YesNo::endTest"<<endl;
	ReciteTestMethodBase::endTest();
}

void ReciteTest_YesNo::resetDynamicButtons()
{
	ReciteTestMethodBase::resetDynamicButtons();

	firstPrompt->hide();
	secondPrompt->hide();
	Yes1->hide();
	Yes2->hide();
	No1->hide();
	No2->hide();
	mTimeBar->hide();

	Yes1ShortCut->setEnabled(false);
	No1ShortCut->setEnabled(false);
	Yes2ShortCut->setEnabled(false);
	No2ShortCut->setEnabled(false);
}

void ReciteTest_YesNo::showNextButton()
{
	curButtonGroup = 3;
	resetDynamicButtons();
	ReciteTestMethodBase::showNextButton();
	//OPT.enableSpeak = enableSpeak;
	//OPT.enableHistoryNav = true;
	//OPT.enableWordBrowserSearch = true;
	OPT.lockArticleNav = false;
	OPT.lockHistoryNav = false;
	OPT.lockSpeak = false;
	OPT.lockWordBrowserSearch = false;
	OPT.lockSwitchTask = false;

	textDisplay->showNavList();
	QString EnglishFormat = QString("<FONT SIZE=5>%1</FONT><BR>");
	QString tip = QString("<FONT COLOR='GREEN'>[BASIC TEST]</FONT>");
	//if (WM.getCandidatesType() == WS_Associate && !WM.getAssociateRoot().isEmpty())
	if (OPT.enableAssociateMap)
	{
		QStringList assoWords = WM.getAssociateWords(WM.getCurWordID());
		if (!assoWords.isEmpty())
		{
			EnglishFormat.append(QString("<FONT COLOR='BLUE'> Associating with "));
			for (int i=0; i<assoWords.size(); i++)
				EnglishFormat.append(QString(" '%1' ").arg(assoWords[i]));
			EnglishFormat.append(QString("</FONT><BR>"));
		}
	}

	BrowseForm history(-1, QString(), tip, EnglishFormat, HT_Test);
	textDisplay->showAll(history);
}

void ReciteTest_YesNo::showFirstYesNoButtonGroup()
{
	//cout<<"ReciteTest_YesNo::showFirstYesNoButtonGroup"<<endl;
	curButtonGroup = 1;
	resetDynamicButtons();

	firstPrompt->show();
	secondPrompt->hide();
	Yes1->show();
	No1->show();
	mTimeBar->show();
	Yes2->hide();
	No2->hide();

	Yes1ShortCut->setEnabled(true);
	No1ShortCut->setEnabled(true);
	Yes2ShortCut->setEnabled(false);
	No2ShortCut->setEnabled(false);

	//Set maximum waiting time
	if (OPT.firstGroupWaitSecMax > 0)
	{
		mTimeBar->setMinimum(0);
		mTimeBar->setMaximum(OPT.firstGroupWaitSecMax*10);
		mTimeBar->setValue(0);
		mTimer->start(100);
	}
}

void ReciteTest_YesNo::showRememberButton()
{
	//cout<<"ReciteTest_YesNo::showRememberButton"<<endl;
	curButtonGroup = 2;
	resetDynamicButtons();

	firstPrompt->hide();
	secondPrompt->show();
	Yes1->hide();
	No1->hide();
	Yes2->show();
	No2->show();
	mTimeBar->show();

	Yes1ShortCut->setEnabled(false);
	No1ShortCut->setEnabled(false);
	Yes2ShortCut->setEnabled(true);
	No2ShortCut->setEnabled(true);

	//OPT.enableSpeak = enableSpeak;
	OPT.lockArticleNav = false;
	//OPT.lockHistoryNav = false;
	OPT.lockSpeak = false;
	//OPT.lockWordBrowserSearch = false;

	textDisplay->showNavList();

	QString EnglishFormat;
	EnglishFormat = QString("<FONT SIZE=5>%1</FONT><BR>");
	//textDisplay->showAll("<FONT COLOR='GREEN'>[BASIC TEST]</FONT>", false, -1, QString(), EnglishFormat);
	BrowseForm history(-1, QString(), QString(), EnglishFormat, HT_Test);
	textDisplay->showAll(history, false);
	//textDisplay->showAll("<FONT COLOR='GREEN'>[BASIC TEST]</FONT>");
	//textDisplay->getTextBrowser()->clear();
	//textDisplay->clearDisplay();
	//textDisplay->showEnglish();
	//textDisplay->showSoundMark();
	//textDisplay->showAllExplanations();
	//textDisplay->display();

	//Set minimum time to wait
	if (OPT.secondGroupWaitSecMin > 0)
	{
		Yes2->setEnabled(false);
		No2->setEnabled(false);
		Yes2ShortCut->setEnabled(false);
		No2ShortCut->setEnabled(false);

		mTimeBar->setMinimum(0);
		mTimeBar->setMaximum(OPT.secondGroupWaitSecMin*10);
		mTimeBar->setValue(0);
		mTimer->start(100);
	}
}

void ReciteTest_YesNo::updateTimeBar()
{
	//cout<<"ReciteTest_YesNo::updateTimeBar"<<endl;
	if (OPT.isPaused)
		return;

	int curVal = mTimeBar->value();
	curVal++;
	mTimeBar->setValue(curVal);

	if (curVal >= mTimeBar->maximum() && curButtonGroup == 1)
	{
		cout<<"ReciteTest_YesNo::updateTimeBar-->Time out. You failed the test."<<endl;
		mTimer->stop();
		forget();
	}
	else if (curVal >= mTimeBar->maximum() && curButtonGroup == 2)
	{
		Yes2->setEnabled(true);
		No2->setEnabled(true);
		Yes2ShortCut->setEnabled(true);
		No2ShortCut->setEnabled(true);
		mTimer->stop();
	}
}
